/*
 * Created on Sep 23, 2009
 * This software is for the designated users only and contains privileged, proprietary or private information. 
 * Any other use of it is prohibited.  
 */
package com.googlecode.xbeandatasource.common;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.betwixt.IntrospectionConfiguration;
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;


/**
 * @author Johny Huang
 *
 */
public class BeanWriterFactory {

	public final static BeanWriter newBeanWriter(OutputStream outPutStream,Object obj) throws IOException, SAXException, IntrospectionException{
	    BeanWriter beanWriter = new BeanWriter(outPutStream);
	    
	    beanWriter.writeXmlDeclaration("<?xml version='1.0' ?>");
	    IntrospectionConfiguration conf=new IntrospectionConfiguration();
	    conf.setAttributesForPrimitives(false);
	    conf.setWrapCollectionsInElement(false);
	    //conf.setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
	    beanWriter.getXMLIntrospector().setConfiguration(conf);
	    beanWriter.getBindingConfiguration().setMapIDs(false);
	    beanWriter.enablePrettyPrint();
	    beanWriter.write(obj.getClass().getName(), obj);
	    beanWriter.flush();
	    
		return beanWriter;
	}

}
